var t = require("../../../@babel/runtime/helpers/interopRequireDefault").default,
  a = require("../../../@babel/runtime/helpers/toConsumableArray"),
  e = require("../../../utils/http"),
  n = t(require("../../../utils/util")),
  o = new e.HTTP;
let {
  intention, 
  getGjzc
} = require("../../../api/attract/select")
let { 
  followBC, 
} = require("../../../api/attract/qd_content")
Page({
  data: {
    followDetail: {},
    nowTextd: '',
   
    hfsj: "请选择日期",
    followDate: '跟进时间',
    selectArray: [{
      id: 1,
      text: "上门拜访"
    }, {
      id: 2,
      text: "来访接待"
    }, {
      id: 3,
      text: "电话拜访"
    }, {
      id: 4,
      text: "QQ微信联系"
    }],
    selectArray3: [{
      id: 1,
      text: "电话号码"
    }, {
      id: 2,
      text: "微信号"
    }],
    liaison: [],
    sortType: 1,
    nowText: "上门拜访",
    selectArrays: [],
    sortTypes: 1,
    nowTexta: "",
    infor: [],
    textValue: '请输入跟进内容',
    gjsj: "请选择跟进时间",
    currentDate: (new Date).getTime(),
    minDate: (new Date).getTime(),
    formatter: function (t, a) {
      return "year" === t ? "".concat(a, "年") : "month" === t ? "".concat(a, "月") : a
    },
    preventTouchMove: function (t) {},
    show: !1,
    currentDates: (new Date).getTime(),
    minDates: (new Date).getTime() - 2592e6,
    maxDates: (new Date).getTime(),
    formatters: function (t, a) {
      return "year" === t ? "".concat(a, "年") : "month" === t ? "".concat(a, "月") : a
    },
    shows: !1,
    manner: !0
  },
  
  handelItemChange: function (t) {
    var a = t.detail.value;
    this.setData({
      checkedList: a
    })
  },
  onConfirm: function (t) {
    var a = n.default.formatStamp(t.detail);
    this.setData({
      show: !1,
      hfsj: a
    })
  },
  onConfirms: function (t) {
    var a = n.default.formatStamp(t.detail);
    console.log(a);
    this.setData({
      shows: !1,
      gjsj: a
    })
  },
  onCancel: function () {
    this.setData({
      show: !1
    })
  },
  quxiao: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  onCancels: function () {
    this.setData({
      shows: !1
    })
  },
  opentime: function () {
    this.setData({
      show: !0
    })
  },
  opentimes: function () {
    this.setData({
      shows: !0
    })
  },
  // 限制时间范围
  rangeTime() {
    const today = new Date();
    const minTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 0, 0);
    const maxTime = new Date(today.getFullYear(), today.getMonth(), today.getDate(), 23, 59);
    return {
      minTime,
      maxTime
    }
  },
  selectEvent: function (t) {
    console.log(t);
    const {
      maxTime,
      minTime
    } = this.rangeTime()
    if (String(t.detail.selectedID) === '1' || String(t.detail.selectedID) === '2') {
      this.setData({
        gjsj: String(n.default.formatExpects(new Date)),
        maxDates: maxTime.getTime(),
        minDates: minTime.getTime()
      })
    } else {
      this.setData({
        gjsj: '请选择跟进时间',
        minDates: (new Date).getTime() - 2592e6,
        maxDates: (new Date).getTime(),
      })
    }
    console.log(t.detail.selectedID)
    this.setData({
        sortType: t.detail.selectedID,
        nowText: t.detail.nowText
      }),
      "电话拜访" == this.data.nowText || "QQ微信联系" == this.data.nowText ? this.setData({
        manner: !1
      }) : this.setData({
        manner: !0
      })
  },

  selectContact: function (t) {
    this.setData({
      liType: t.detail.selectedID,
      nowTexts: t.detail.nowText
    })
  },
  selectEvent2: function (t) {
    this.setData({
      sortTypes: t.detail.selectedID,
      nowTexta: t.detail.nowText
    })
  },

  formSubmit: function (t) {
    // 校验定位打卡 跟进内容
    // if (this.data.nowText === '来访接待' || this.data.nowText === '上门拜访') {
    //   if (!wx.getStorageSync('clocks')) {
    //     return wx.showToast({
    //       title: "请检查是否有必填项未填",
    //       icon: "none",
    //       duration: 1e3
    //     })
    //   }
    // }
    if (t.detail.value.genjin.length < 10) {
      return wx.showToast({
        title: '跟进内容不得小于10个字',
        icon: 'none',
        duration: 1e3
      })
    }
     
    var a = this
    e = t.detail.value;

    wx.showModal({
      title: "提示",
      content: "请确认是否提交",
      success:  function (t) {
        o.request({
          url: "https://www.qlsyyun.com:3000/getUserCharge",
          data: {
            bm: wx.getStorageSync("user")[1]
          },
        }).then(res => {
          // 跟进记录存进sxtx发送提醒
          if (res[0].nr !== wx.getStorageSync("user")[0]) {
            o.request({
              url: 'https://www.qlsyyun.com:3000/setFollowSxtx',
              method: 'POST',
              header: {
                "Content-Type": "application/x-www-form-urlencoded"
              },
              data: {
                qymc: a.data.infor.qymc,
                khbh: a.data.infor.khbh,
                jname: a.data.infor.zsy,
                nr: res[0].nr,
                bm: a.data.infor.bm,
                gjlx: a.data.nowText,
                gjnr: e.genjin,
                bid: a.data.bID
              }
            }).then(res => {
              console.log(res);
            })
          }
        })


        "来访接待" == a.data.nowText || "上门拜访" == a.data.nowText ? a.setData({
            khzt: "拜访"
          }) : a.setData({
            khzt: "联系"
          }),
          null == a.data.checkedList && a.setData({
            checkedList: " "
          }),
         
          "上门拜访" == a.data.nowText ? null != a.data.nowTexts && e.genjin && "请选择日期" != a.data.hfsj &&
          a.data.clock ? (o.request({
              url: "https://www.qlsyyun.com:3000/follow/lxcs",
              data: {
                khbh: a.data.infor.khbh
              }
            }).then((function (t) {
              if (t.data.length > 0) {
                var e = t.data[0].lxcs;
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/lxrb",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    lxcs: e + 1,
                    khbh: a.data.infor.khbh
                  }
                }).then((function (t) {}))
              } else
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/mylxrb",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    qymc: a.data.infor.qymc,
                    xm: a.data.nowTexts,
                    sj: a.data.infor.lxphone,
                    tname: a.data.name[0],
                    khbh: a.data.infor.khbh,
                    tjsj: a.data.nowtime
                  }
                }).then((function (t) {}))
            })),
            followBC({
              ID: a.data.followDetail.ID,
              bID: a.data.bID, 
              gjid: a.data.followDetail.gjid,
              jhid: a.data.followDetail.jhid,
              // qymc: a.data.followDetail.qymc,
              lxr: a.data.nowTexts,
              gjlx: a.data.nowText,
              gjsj: a.data.gjsj,
              gjnr: e.genjin,
              // tname: a.data.name[0],
              // tjsj: a.data.nowtime,
              // bm: a.data.name[1],
              khbh: a.data.followDetail.khbh,
              khjd: a.data.nowTexta,
              zsph: a.data.checkedList,
              khly: wx.getStorageSync('infor').khly?wx.getStorageSync('infor').khly:'渠道'
            }).then((function (t) {

              "核名" == a.data.khjda && a.setData({
                  nowTexta: "核名"
                }),
                // "强烈意向" == a.data.nowTexta && a.setData({
                //   nowTexta: "强烈意向"
                // }),
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/zscb",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    bID: a.data.bID,
                    gjsj: a.data.gjsj,
                    tjsj: a.data.nowtime,
                    khjd: a.data.nowTexta,
                    zxgj: n.default.formatExpects(new Date) + a.data.nowText,
                    khyxa: a.data.nowTexta
                  }
                }).then((function (t) {})),
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/zsjd",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    bm: a.data.name[1],
                    qymc: a.data.infor.qymc,
                    khbh: a.data.infor.khbh,
                    zsy: a.data.name[0],
                    khzt: a.data.khzt,
                    gjlx: a.data.nowText,
                    tjsj: a.data.nowtime
                  }
                }).then((function (t) {})),
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/zsjh",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    bm: a.data.name[1],
                    qymc: a.data.infor.qymc,
                    khbh: a.data.infor.khbh,
                    zsy: a.data.name[0],
                    tjsj: a.data.nowtime,
                    hfsj: a.data.hfsj,
                    bID: a.data.bID,
                    hid: a.data.hid
                  }
                }).then((function (t) {})),
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/stjd",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    khbh: a.data.infor.khbh,
                    qymc: a.data.infor.qymc,
                    zsy: a.data.name[0],
                    bID: a.data.bID,
                    bm: a.data.name[1],
                    tjsj: a.data.nowtime,
                    gjlx: a.data.nowText
                  }
                }).then((function (t) {})),

                o.request({
                  url: "https://www.qlsyyun.com:3030/clock/get",
                  data: {
                    arr: 4,
                    khbh: a.data.infor.khbh
                  }
                }).then((function (t) { 
                  o.request({
                      url: "https://www.qlsyyun.com:3030/follow/image",
                      header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                      },
                      method: "POST",
                      data: {
                        mingc: a.data.address,
                        hid: a.data.infor.bID,
                        FileUrl: a.data.address,
                        khbh: a.data.infor.khbh,
                        tname: a.data.name[0],
                        gid: t.data[0].ID
                      }

                    }).then((function (t) {
                      wx.removeStorageSync("clocks"),
                        wx.removeStorageSync("nowaddress"),
                        wx.removeStorageSync("addressgj"),
                        wx.removeStorageSync("clockTime"),
                        wx.showToast({
                          title: "提交成功",
                          icon: "none",
                          duration: 1000,
                          mask: !0,
                          success: function () {
                            setTimeout((function () {
                              // wx.redirectTo({
                              //   url: `/pages/intention/intention/intention?address='跟进记录'&id=7`
                              // })
                              
                              let pages = getCurrentPages()
                              let beforePage = pages[pages.length - 2];
                              // 假设main()为页面A的数据方法，
                              beforePage.onLoad({
                                id: 1,
                                qdid: a.data.infor.bID
                              });
                              wx.navigateBack({
                                delta: 1,
                              })
                            }), 1e3)
                          }
                        })
                    })),
                    o.request({
                      url: "https://www.qlsyyun.com:3030/clock/jifen",
                      header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                      },
                      method: "POST",
                      data: {
                        image: a.data.address,
                        qymc: a.data.infor.qymc,
                        bm: a.data.name[1],
                        zsy: a.data.name[0],
                        dksj: a.data.clockTime,
                        address: a.data.location,
                        khbh: a.data.infor.khbh,
                        gjid: t.data[0].ID
                      }
                    }).then((function (t) {}))
                }))
            }))) : wx.showToast({
            title: "请检查是否有必填项未填",
            icon: "none",
            duration: 1e3
          }) : null != a.data.nowTexts && e.genjin && "请选择跟进时间" != a.data.gjsj && "请选择日期" != a.data.hfsj ? (o.request({
              url: "https://www.qlsyyun.com:3000/follow/lxcs",
              data: {
                khbh: a.data.infor.khbh
              }
            }).then((function (t) {
              if (t.data.length > 0) {
                var e = t.data[0].lxcs;
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/lxrb",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    lxcs: e + 1,
                    khbh: a.data.infor.khbh
                  }
                }).then((function (t) {}))
              } else
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/mylxrb",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    qymc: a.data.infor.qymc,
                    xm: a.data.nowTexts,
                    sj: a.data.infor.lxphone,
                    tname: a.data.name[0],
                    khbh: a.data.infor.khbh,
                    tjsj: a.data.nowtime
                  }
                }).then((function (t) {}))
            })),
            // o.request({
            //   url: "https://www.qlsyyun.com:3000/follow",
            //   header: {
            //     "Content-Type": "application/x-www-form-urlencoded"
            //   },
            //   method: "POST",
            //   data: {
            //     bID: a.data.bID,
            //     qymc: a.data.infor.qymc,
            //     lxr: a.data.nowTexts,
            //     gjlx: a.data.nowText,
            //     gjsj: a.data.gjsj,
            //     gjnr: e.genjin,
            //     tname: a.data.name[0],
            //     tjsj: a.data.nowtime,
            //     bm: a.data.name[1],
            //     khbh: a.data.infor.khbh,
            //     khjd: a.data.nowTexta,
            //     zsph: a.data.checkedList,
            //     khly:wx.getStorageSync('infor').khly
            //   }
            // })
            followBC({
              ID: a.data.followDetail.ID, 
              bID: a.data.bID,
              // qymc: a.data.infor.qymc,
                 gjid: a.data.followDetail.gjid,
              jhid: a.data.followDetail.jhid,
              lxr: a.data.nowTexts,
              gjlx: a.data.nowText,
              gjsj: a.data.gjsj,
              gjnr: e.genjin,
              // tname: a.data.name[0],
              // tjsj: a.data.nowtime,
              // bm: a.data.name[1],
              khbh: a.data.followDetail.khbh,

              khjd: a.data.nowTexta,
              zsph: a.data.checkedList,
              khly: wx.getStorageSync('infor').khly?wx.getStorageSync('infor').khly:'渠道'
            }).then((function (t) {
              "核名" == a.data.khjda && a.setData({
                  nowTexta: "核名"
                }),
                // "强烈意向" == a.data.nowTexta && a.setData({
                //   nowTexta: "强烈意向"
                // }),
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/zscb",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    bID: a.data.bID,
                    gjsj: a.data.gjsj,
                    tjsj: a.data.nowtime,
                    khjd: a.data.nowTexta,
                    zxgj: a.data.gjsj + a.data.nowText,
                    khyxa: a.data.nowTexta
                  }
                }).then((function (t) {})),
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/zsjd",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    bm: a.data.name[1],
                    qymc: a.data.infor.qymc,
                    khbh: a.data.infor.khbh,
                    zsy: a.data.name[0],
                    khzt: a.data.khzt,
                    gjlx: a.data.nowText,
                    tjsj: a.data.nowtime
                  }
                }).then((function (t) {})),
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/zsjh",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    bm: a.data.name[1],
                    qymc: a.data.infor.qymc,
                    khbh: a.data.infor.khbh,
                    zsy: a.data.name[0],
                    tjsj: a.data.nowtime,
                    hfsj: a.data.hfsj,
                    bID: a.data.bID,
                    hid: a.data.hid
                  }
                }).then((function (t) {})),
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/stjd",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    khbh: a.data.infor.khbh,
                    qymc: a.data.infor.qymc,
                    zsy: a.data.name[0],
                    bID: a.data.bID,
                    bm: a.data.name[1],
                    tjsj: a.data.nowtime,
                    gjlx: a.data.nowText
                  }
                }).then((function (t) {})),
                o.request({
                  url: "https://www.qlsyyun.com:3000/follow/stmx",
                  header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                  },
                  method: "POST",
                  data: {
                    khbh: a.data.infor.khbh,
                    qymc: a.data.infor.qymc,
                    zsy: a.data.name[0],
                    bID: a.data.bID,
                    bm: a.data.name[1],
                    tjsj: a.data.nowtime,
                    gjlx: a.data.nowText
                  }
                }).then((function (t) {})),
                o.request({
                  url: "https://www.qlsyyun.com:3030/clock/get",
                  data: {
                    arr: 4,
                    khbh: a.data.infor.khbh
                  }
                }).then((function (t) {
                  a.setData({
                      jdid: t.data[0].ID
                    }),

                    wx.showToast({
                      title: "提交成功",
                      icon: "none",
                      duration: 1e3,
                      mask: !0,
                      success: function () {
                        setTimeout((function () {
                          // wx.redirectTo({
                          //   url: `/pages/intention/intention/intention?address='跟进记录'&id=7`
                          // })
                        
                          let pages = getCurrentPages()
                          let beforePage = pages[pages.length - 2];
                          // 假设main()为页面A的数据方法，
                          beforePage.onLoad({
                            id: 1
                          });
                          wx.navigateBack({
                            delta: 1,
                          })

                        }), 1e3)
                      }
                    }),
                    "来访接待" == a.data.nowText && (a.data.clock ? o.request({
                      url: "https://www.qlsyyun.com:3030/clock/jifen",
                      header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                      },
                      method: "POST",
                      data: {
                        image: a.data.address,
                        qymc: a.data.infor.qymc,
                        bm: a.data.name[1],
                        zsy: a.data.name[0],
                        dksj: a.data.clockTime,
                        address: a.data.location,
                        khbh: a.data.infor.khbh,
                        gjid: a.data.jdid
                      }
                    }).then((function (t) {})) : o.request({
                      url: "https://www.qlsyyun.com:3030/clock/record",
                      header: {
                        "Content-Type": "application/x-www-form-urlencoded"
                      },
                      method: "POST",
                      data: {
                        qymc: a.data.infor.qymc,
                        bm: a.data.name[1],
                        zsy: a.data.name[0],
                        gjid: a.data.jdid
                      }
                    }).then((function (t) {})))
                }))
            }))) : wx.showToast({
            title: "请检查是否有必填项未填",
            icon: "none",
            duration: 1e3
          })
      }
    })
  },
  formatTime(Time,type=1) {
    // console.log(Time)
    let date = new Date(Time)
    const year = date.getFullYear();
    const month = (date.getMonth() + 1).toString().padStart(2, '0');
    const day = date.getDate().toString().padStart(2, '0');
    const hour = date.getHours().toString().padStart(2, '0');
    const minute = date.getMinutes().toString().padStart(2, '0');
    const second = date.getSeconds().toString().padStart(2, '0');
    if(type==1){
      return `${year}-${month}-${day}`;

    }else if(type==2){
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
    }else if(type==3){
      return `${hour}:${minute}:${second}`;
          }
    
  },
  onLoad: function (t) {
    getGjzc(t.bid,t.tjsj).then(res => {
      console.log(res)
      this.setData({
        followDetail: res.data[0]
      })
   
    this.setData({
      clock: this.formatTime(this.data.followDetail.gjsj,3),
      address: this.data.followDetail.iamge,
      location: this.data.followDetail.address,
      clockTime: this.formatTime(this.data.followDetail.gjsj,2)
    })
    })
    var e = this,
      d = n.default.formatnow(new Date),
      s = wx.getStorageSync("infor"),
      r = wx.getStorageSync("user");
    const {
      maxTime,
      minTime
    } = this.rangeTime()
    e.setData({
      gjsj: String(n.default.formatExpects(new Date)),
      maxDates: maxTime.getTime(),
      minDates: minTime.getTime()
    }) 
    intention(17).then(res=>{
      res.data.forEach(item=>{
        item.text=item.nr
        item.id=item.pxbh
      })
      e.setData({
        selectArrays: res.data,
        sortTypes:1,
        nowTexta:res.data[0].text
      })
    })
    o.request({
        url: "https://www.qlsyyun.com:3000/follow/khjd",
        data: {
          bID: t.bid
        }
      }).then((function (t) {
        e.setData({
          khjda: t.data[0].khjd
        })
        console.log(e.data.khjda);
      })),
      o.request({
        url: "https://www.qlsyyun.com:3000/maintain/liaison",
        data: {
          khbh: s.khbh
        }
      }).then((function (t) {
        e.setData({
          liaison: t.data
        })
      })),
      o.request({
        url: "https://www.qlsyyun.com:3000/maintain/visit",
        data: {
          arr: 1,
          bumen: r[1],
          class: r[4]
        }
      }).then((function (t) {
        var n = t.data;
        if (r[1] == '达博' || r[1] == "江环项目" || r[1] == "科澜项目") {
          let xmgs = r[7].split(',')
          let newXmgs = []
          if (xmgs[0] !== '') {
            xmgs.forEach(item => {
              o.request({
                url: "https://www.qlsyyun.com:3000/maintain/visit",
                data: {
                  arr: 3,
                  bumen: item,
                  class: r[4],
                  zsy: r[0]
                }
              }).then(res => {
                newXmgs.push(...res.data)
                e.setData({
                  visitpeo: [...n, ...newXmgs]
                })
              })
            });
          } else {
            o.request({
              url: "https://www.qlsyyun.com:3000/maintain/visit",
              data: {
                arr: 3,
                bumen: r[1],
                class: r[4],
                zsy: r[0]
              }
            }).then(res => {
              newXmgs.push(...res.data)
              e.setData({
                visitpeo: [...n, ...newXmgs]
              })
            })
          }

        } else {

          let xmgs = r[7].split(',')
          let newXmgs = []
          if (xmgs[0] !== '') {
            xmgs.forEach(item => {
              o.request({
                url: "https://www.qlsyyun.com:3000/maintain/visit",
                data: {
                  arr: 2,
                  bumen: item,
                  class: r[4],
                  zsy: r[0]
                }
              }).then(res => {
                newXmgs.push(...res.data)
                e.setData({
                  visitpeo: [...n, ...newXmgs]
                })
                console.log(e.data.visitpeo);
              })
            })
          } else {
            o.request({
              url: "https://www.qlsyyun.com:3000/maintain/visit",
              data: {
                arr: 2,
                bumen: r[1],
                class: r[4],
                zsy: r[0]
              }
            }).then(res => {
              newXmgs.push(...res.data)
              e.setData({
                visitpeo: [...n, ...newXmgs]
              })
            })
          }

        }
      })),
      o.request({
        url: "https://www.qlsyyun.com:3000/jhID"
      }).then((function (t) {
        var a = n.default.formatTime(new Date).substring(17, 19),
          o = n.default.formatDay(new Date),
          d = "h" + n.default.formatDates(new Date) + o + a + (t.data[0].jhID + 1);
        e.setData({
          jhID: t.data[0].jhID,
          hid: d
        })
        console.log(e.data.hid);
      })),
      e.setData({
        infor: s,
        bID: s.bID,
        nowtime: d,
        name: r
      })
  },
  onShow: function () {
   
  },
  locateBtn: function () {
    wx.navigateTo({
      url: `/pages/locate/follow/follow?sortType=${this.data.sortType}`
    })
  }
});